/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client;


import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.activity.shared.CachingActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimpleLayoutPanel;
import com.weissenborn.colabtex.client.mapper.CollabTexPlaceHistoryMapper;
import com.weissenborn.colabtex.client.mapper.ContentActivityMapper;
import com.weissenborn.colabtex.client.mapper.HeaderActivityMapper;
import com.weissenborn.colabtex.client.mapper.MainActivityMapper;
import com.weissenborn.colabtex.client.mapper.SidebarActivityMapper;
import com.weissenborn.colabtex.client.places.Dashboard;
import com.weissenborn.colabtex.client.places.Login;
import com.weissenborn.colabtex.client.service.UserManagementService;
import com.weissenborn.colabtex.client.service.UserManagementServiceAsync;
import com.weissenborn.colabtex.client.view.MainView;


/**
 * The Class ColabTex which provides the entry point of this application
 */
public class ColabTex implements EntryPoint {
		
		/** The default place. */
		private Place defaultPlace = new Login(new Dashboard());
	    
    	/** The app panel. */
    	private SimpleLayoutPanel appPanel = new SimpleLayoutPanel();
	    

	    /* (non-Javadoc)
    	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
    	 */
    	public void onModuleLoad() {
	    	init();
	        ClientFactory clientFactory = GWT.create(ClientFactory.class);
	        clientFactory.addHandlers();
	        EventBus eventBus = clientFactory.getEventBus();
	        PlaceController placeController = clientFactory.getPlaceController();	        
	     
	      
	        // Start ActivityManager for the main widget with our ActivityMapper
	        ActivityMapper mainActivityMapper = new MainActivityMapper(clientFactory);
	        ActivityManager mainActivityManager = new ActivityManager(mainActivityMapper, eventBus);
	        mainActivityManager.setDisplay(appPanel);
	        
	        MainView mainview = clientFactory.getMainView();

	        // Start ActivityManager for the main widget with our ActivityMapper
	        ActivityMapper contentActivityMapper = new ContentActivityMapper(clientFactory);
	        ActivityManager contentActivityManager = new ActivityManager(contentActivityMapper, eventBus);
	        contentActivityManager.setDisplay(mainview.getContentPanel());
	        
	        ActivityMapper sidebarActivityMapper = new SidebarActivityMapper(clientFactory);
	        ActivityManager sideBarActivityManager = new ActivityManager(new CachingActivityMapper(sidebarActivityMapper), eventBus);
	        sideBarActivityManager.setDisplay(mainview.getSidebarPanel());
	        
	        ActivityMapper headerActivityMapper = new HeaderActivityMapper(clientFactory);
	        ActivityManager headerActivityManager = new ActivityManager(new CachingActivityMapper(headerActivityMapper), eventBus);
	        headerActivityManager.setDisplay(mainview.getHeaderPanel());
	        
	        // Start PlaceHistoryHandler with our PlaceHistoryMapper
	        CollabTexPlaceHistoryMapper historyMapper= GWT.create(CollabTexPlaceHistoryMapper.class);
	        PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
	        historyHandler.register(placeController, eventBus, defaultPlace);

	        RootLayoutPanel.get().add(appPanel);
	        // Goes to the place represented on URL else default place
	        historyHandler.handleCurrentHistory();	
	}


	/**
	 * Inits the.
	 */
	private void init() {
		UserManagementServiceAsync service = UserManagementService.Util.getInstance();

		AsyncCallback<Void> callback = new AsyncCallback<Void>() {

			public void onFailure(Throwable caught) {
				Window.alert("No connection to server!");
			}

			public void onSuccess(Void result) {			
			}
		};		
		service.initShiro(callback);
	}

}
